package org.zhouhe.util;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HTML {
	public static void main(String[] args) {
		createHtmlFile("http://localhost/zpd0t1_db/index.action", "d:/index.html");
	}
	public static void createHtmlFile(String url, String path){
		
		try {
			String html = getHttpText(url);
			File f = new File(path);
			FileOutputStream fos = new FileOutputStream(f);
			fos.write(html.getBytes());
			fos.flush();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	public static String getHttpText(String str) {
        URL url = null;
        try {
            url = new URL(str);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HttpURLConnection huc = null;
        try {

            huc = (HttpURLConnection) url.openConnection();
            huc.setRequestMethod("GET");
            huc.setRequestProperty("Content-Type",
                    "application/vnd.syncml.dm+xml");
            // application/x-www-form-urlencoded
            // application/vnd.syncml.dm+xml
            huc.setDoOutput(true);
            huc.setRequestProperty("Cache-Control", "private");
            huc.setRequestProperty("Accept-Charset", "utf-8");
            huc.setRequestProperty("Accept", "application/vnd.syncml.dm+xml");
            huc.setRequestProperty("Content-Length", "3");
            DataOutputStream printout;
            printout = new DataOutputStream(huc.getOutputStream());
            printout.writeBytes("abc");
        } catch (IOException e) {
            e.printStackTrace();
        }
        InputStream is = null;
        try {
            is = huc.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuffer sb = new StringBuffer();
        String line = "";
        try {
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}
